//启动页面
import 'dart:async';
import 'dart:io';

import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/home_cover_request.dart';
import 'package:elephant_fish/model/app_cover_entity.dart';
import 'package:elephant_fish/page/home_page.dart';
import 'package:elephant_fish/page/login_page.dart';
import 'package:elephant_fish/util/size_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


class LaunchPage extends StatefulWidget {
  const LaunchPage({Key key}) : super(key: key);

  @override
  _LaunchPageState createState() => _LaunchPageState();
}

class _LaunchPageState extends State<LaunchPage> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return LaunchPageWidget();
  }


}


class LaunchPageWidget extends StatefulWidget {

  const LaunchPageWidget({Key key,}) : super(key: key);
  @override
  State<StatefulWidget> createState() => LaunchState();
}

class LaunchState extends State<LaunchPageWidget> {
  int _countdown = 5;
  Timer _countdownTimer;
  List<AppCoverEntity> appCoverEntities;
  @override
  void initState() {
    getHomeCover();
    super.initState();
    print('初始化启动页面');
  }

  @override
  void dispose() {
    super.dispose();
    print('启动页面结束');
    if (_countdownTimer != null && _countdownTimer.isActive) {
      _countdownTimer.cancel();
      _countdownTimer = null;
    }
  }

  void _startRecordTime() {
    _countdownTimer = Timer.periodic(Duration(seconds: 1), (timer) {
      setState(() {
        if (_countdown <= 1) {
//          Navigator.of(context).pushNamed("/demo1");
          Navigator.of(context).pop();
          Navigator.of(context).push(MaterialPageRoute(builder: (context) {
            return LoginPage();
          }));
          _countdownTimer.cancel();
          _countdownTimer = null;
        } else {
          _countdown -= 1;
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Color(0xffffffff),
        body: Stack(
          fit: StackFit.expand,
          children: [
            appCoverEntities == null ? Container(width: 0,height: 0,) :
            appCoverEntities.length > 0 ?
            Stack(
              children: [
                Column(
                  children: [
                    Row(
                      children: [
                        Column(
                          children: [
                            Image.network(
                              'http://119.28.131.94:82' + appCoverEntities[2].cover,
                              width: size.width * 0.37,
                              height: size.height * 0.2,
                              fit: BoxFit.cover,
                            ),
                            Image.network(
                              'http://119.28.131.94:82' + appCoverEntities[3].cover,
                              width: size.width * 0.37,
                              height: size.height * 0.3,
                              fit: BoxFit.cover,
                            ),
                          ],
                        ),
                        Image.network(
                          'http://119.28.131.94:82' + appCoverEntities[0].cover,
                          width: size.width * 0.63,
                          height: size.height * 0.5,
                          fit: BoxFit.cover,
                        ),
                      ],
                    ),
                    Row(
                      children: [
                        Image.network(
                          'http://119.28.131.94:82' + appCoverEntities[1].cover,
                          width: size.width * 0.63,
                          height: size.height * 0.5,
                          fit: BoxFit.cover,
                        ),
                        Column(
                          children: [
                            Image.network(
                              'http://119.28.131.94:82' + appCoverEntities[4].cover,
                              width: size.width * 0.37,
                              height: size.height * 0.25,
                              fit: BoxFit.cover,
                            ),
                            Image.network(
                              'http://119.28.131.94:82' + appCoverEntities[5].cover,
                              width: size.width * 0.37,
                              height: size.height * 0.25,
                              fit: BoxFit.cover,
                            ),
                          ],
                        ),
                      ],
                    )
                  ],
                )
              ],
            ) :
            Stack(
              children: [
                Center(
                  child: Image.asset('assets/images/launch_logo.png'),

                ),
                Align(
                  alignment: Alignment.bottomCenter,
                  child: Padding(
                    padding: EdgeInsets.only(bottom: SizeUtil.px(45)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset('assets/images/logo_red.png',width: SizeUtil.px(40.2),height: SizeUtil.px(39),),
                        SizedBox(width: SizeUtil.px(19),),
                        Text('象鱼',style: TextStyle(fontSize: SizeUtil.px(39),color: Color(0xff0c0c0c))),

                      ],
                    ),
                  ),
                ),
              ],
            ),

            Positioned(
              top: 30,
              right: 30,
              child: InkWell(
                child: Container(
                  padding: EdgeInsets.fromLTRB(5, 2, 5, 2),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: Colors.black12,
                  ),
                  child: RichText(
                    text: TextSpan(children: [
                      TextSpan(
                          text: '$_countdown',
                          style: TextStyle(
                            fontSize: 18,
                            color: Colors.grey,
                          )),
                      TextSpan(
                          text: '跳过',
                          style: TextStyle(
                            fontSize: 18,
                            color: Colors.grey,
                          )),
                    ]),
                  ),
                ),
                onTap: (){
                  Navigator.of(context).pop();
                  Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                    return LoginPage();
                  }));
                  _countdownTimer.cancel();
                  _countdownTimer = null;
                },
              )
            )
          ],
        ),
      ),
    );
  }

  ///获取首页封面
  getHomeCover() async {
    BaseRequest request = HomeCoverRequest();
    var result = await HiNet.getInstance().fire(request);
    if (result['code'] == 0) {
      List<Map<String, dynamic>> listMap = new List<Map<String, dynamic>>.from(result['data']);
      List<AppCoverEntity> list = List();
      for (int i = 0; i < listMap.length; i++) {
        list.add(AppCoverEntity.fromJson(listMap[i]));
      }
      setState(() {
        appCoverEntities = list;
        _startRecordTime();
      });
    }
  }
}
